﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Xml;

namespace Restart.Installer.CustomActions
{
    [RunInstaller(true)]
    public partial class ModifyConfigFile : System.Configuration.Install.Installer
    {
        public ModifyConfigFile()
        {
            InitializeComponent();
        }

        public override void Install(IDictionary stateSaver)
        {
            string targetDir = Context.Parameters["targetDir"];
            string configFileName = Context.Parameters["configFile"];
            string serverOrClient = Context.Parameters["serverOrClient"];
            string server = Context.Parameters["server"];
            string password = Context.Parameters["password"];

            if (string.IsNullOrEmpty(targetDir))
                throw new InstallException("Target directory was not provided to set configuration values.");

            if (string.IsNullOrEmpty(configFileName))
                throw new InstallException("Config file was not provided to set configuration values.");

            FileInfo configFile = new FileInfo(Path.Combine(targetDir, configFileName));

            if (!configFile.Exists)
                throw new InstallException(string.Format("Config file does not exist: {0}", configFile.FullName));

            XmlDocument doc = new XmlDocument();
            doc.Load(configFile.FullName);

            if (serverOrClient == "server")
                ModifyServerConfig(doc, server, password);
            else
                ModifyClientConfig(doc, server, password);

            doc.Save(configFile.FullName);
        }

        private void ModifyServerConfig(XmlDocument doc, string server, string password)
        {
            // Modify the base address node
            XmlNode baseAddressAttribute = doc.SelectSingleNode(@"//baseAddresses/add/@baseAddress");
            baseAddressAttribute.InnerText = baseAddressAttribute.InnerText.Replace("my-home-server", server);

            // Modify password
            XmlNode passwordNode = doc.SelectSingleNode(@"//setting[@name='Password']/value");
            passwordNode.InnerText = password;
        }

        private void ModifyClientConfig(XmlDocument doc, string server, string password)
        {
            // Modify the endpoint address node
            XmlNode endpointNode = doc.SelectSingleNode(@"//client/endpoint");            
            XmlNode addressAttribute = endpointNode.SelectSingleNode(@"@address");
            addressAttribute.InnerText = addressAttribute.InnerText.Replace("my-home-server", server);

            // Modify the identity node of the endpoint
            XmlNode principalNameValueAttribute = endpointNode.SelectSingleNode(@"//identity/servicePrincipalName/@value");
            principalNameValueAttribute.InnerText = principalNameValueAttribute.InnerText.Replace("my-home-server", server);

            // Modify password
            XmlNode passwordNode = doc.SelectSingleNode(@"//setting[@name='Password']/value");
            passwordNode.InnerText = password;
        }
    }
}
